<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}

$act = $_G['gp_act'];

if( $act == 'get_msgs' ){
	$page = $_G['gp_page'] ? $_G['gp_page'] : 1;
	$where = array(
		'receiverid' => $admincp->adminid,
//		'read' => 0
	);
	$options = array(
		'order' => 'ORDER BY `create_time` DESC'
	);

	$total = DB::Count('admincp_message', array(
		'receiverid' => $admincp->adminid,
	));
	$unread = DB::Count('admincp_message', array(
		'receiverid' => $admincp->adminid,
		'read' => 0
	));
	$count = DB::Count('admincp_message', $where);
	$pages = pages($count, 20);
	$msgs = limitquery('admincp_message', $where, $options, $pages['offset'], $pages['pagesize']);
	$mailids = idserialize($msgs, 'mailid');

	$mails = parse_array(limitquery('admincp_mail', "`id` IN('{$mailids}')"));
	$adminids = idserialize($mails, 'adminid');

	$admins = parse_array(limitquery('admincp_member', "`id` IN('{$adminids}')"));

	foreach( $msgs as $k => $v ){
		$mail = $mails[$v['mailid']];
		$v['adminid'] = $mail['adminid'];
		$v['title'] = $mail['title'];
		$v['description'] = mb_substr(strip_tags($mails[$v['mailid']]['detail']), 0, 128, 'UTF8');
		$v['sender'] = $admins[$mail['adminid']]['realname'] ? $admins[$mail['adminid']]['realname'] : $admins[$mail['adminid']]['username'];
		$v['date'] = date('Y-m-d H:i:s', $v['create_time']);
		$msgs[$k] = $v;
	}
	unset($pages['pages']);

	$datas = array(
		'unread' => $unread,
		'total' => $total,
		'count' => $count,
		'msgs' => $msgs,
		'page' => intval($page),
		'maxpage' => ceil($count / $pages['pagesize']),
		'pages' => $pages
	);
	jsonp($datas, 'data', 0);
}else if( $act == 'get_sents' ){
	$page = $_G['gp_page'] ? $_G['gp_page'] : 1;
	$where = array(
		'adminid' => $admincp->adminid,
	);
	$options = array();

	$count = DB::Count('admincp_mail', $where);
	$pages = pages($count, 20);
	$mails = limitquery('admincp_mail', $where, $options, $pages['offset'], $pages['pagesize']);

	foreach( $mails as $k => $mail ){
		$mail['description'] = mb_substr(strip_tags($mail['detail']), 0, 128, 'UTF8');
		$mail['sender'] = heivrid($admincp->admin);
		$mail['date'] = date('Y-m-d H:i:s', $mail['create_time']);
		$mails[$k] = $mail;
	}
	unset($pages['pages']);

	$datas = array(
		'total' => $count,
		'count' => $count,
		'msgs' => $mails,
		'page' => intval($page),
		'maxpage' => ceil($count / $pages['pagesize']),
		'pages' => $pages
	);
	jsonp($datas, 'data', 0);
}else if( $act == 'read_mail' ){
	$mailid = abs(intval($_G['gp_mailid']));
	$mail = fetch('admincp_mail', $mailid);
	if( !$mail ){
		jsonp('信息不存在');
	}
	$msg = fetch('admincp_message', array(
		'receiverid' => $admincp->adminid,
		'mailid' => $mail['id']
	));
	if( !$msg ){
		jsonp('无权阅读该信息');
	}

	$sender = fetch('admincp_member', $mail['adminid']);

	$mail['date'] = date('Y-m-d H:i:s', $mail['create_time']);

	$data = array(
		'sender' => array(
			'id' => $sender['id'],
			'heivrid' => $sender['username'],
			'name' => heivrid($sender),
			'avatar' => avatar($sender['avatar'])
		),
		'mail' => $mail
	);
	if( !$msg['read'] ){
		update('admincp_message', $msg['id'], array(
			'read' => 1
		));
	}
	jsonp($data, 'data', 0);
}else if( $act == 'read_msg' ){
	$mailid = abs(intval($_G['gp_mailid']));
	$mail = fetch('admincp_mail', $mailid);
	if( !$mail ){
		jsonp('信息不存在');
	}
	$msg = fetch('admincp_message', array(
		'receiverid' => $admincp->adminid,
		'mailid' => $mail['id']
	));
	if( !$msg ){
		jsonp('无权阅读该信息');
	}

	$sender = fetch('admincp_member', $mail['adminid']);

	$mail['date'] = date('Y-m-d H:i:s', $mail['create_time']);

	$data = array(
		'sender' => array(
			'id' => $sender['id'],
			'heivrid' => $sender['username'],
			'name' => heivrid($sender),
			'avatar' => avatar($sender['avatar'])
		),
		'mail' => $mail
	);
	if( !$msg['read'] ){
		update('admincp_message', $msg['id'], array(
			'read' => 1
		));
	}
	jsonp($data, 'data', 0);
}else if( $act == 'post_mail' ){
	$heivrids = $_G['gp_heivrids'];
	$subject = $_G['gp_subject'];
	$detail = $_G['gp_detail'];
	if( !$heivrids ){
		jsonp('请填写接收人');
	}
	if( !$subject ){
		jsonp('请填写标题');
	}
	if( !$detail ){
		jsonp('请填写内容');
	}
	$server = new admincp_mail();
	$server->adminid = $admincp->adminid;
	$server->title = $subject;
	$server->detail = $detail;
	$server->receiverids = $heivrids;
	$server->create();
	jsonp('操作成功', 'data', 0);
}
?>